package com.coderman.business.service.imp;

import com.coderman.business.converter.EmpConverter;
import com.coderman.business.mapper.EmpMapper;
import com.coderman.business.service.IEmpService;
import com.coderman.business.service.base.AbstractServiceImpl;
import com.coderman.common.error.SystemCodeEnum;
import com.coderman.common.error.SystemException;
import com.coderman.common.model.business.Emp;
import com.coderman.common.vo.business.EmpVO;
import com.coderman.common.vo.system.PageVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * 员工业务层实现
 * @author  文晓平
 * @Date 2023-1-9
 * @version 1.0
 */
@Service
public class EmpServiceImpl extends AbstractServiceImpl<EmpMapper, Emp, Integer> implements IEmpService {

    @Autowired
    private EmpMapper empMapper;

    @Override
    public List<Emp> getEmpNameList() {
        return empMapper.selectAll();
    }

    @Override
    public PageVO<Emp> findList(Integer pageNum, Integer pageSize, EmpVO empVO) {
        PageHelper.startPage(pageNum,pageSize);
//        Example o = new Example(Emp.class);
//        String empName = empVO.getEmpname();
//        Integer departmentId = empVO.getDeptid();
//        String moblie = empVO.getMoblie();
//        String sex = empVO.getSex();
//        Integer empstatus = empVO.getEmpstatus();
//        Example.Criteria criteria = o.createCriteria();
//        if(!StringUtils.isEmpty(empName)){
//            criteria.andLike("empname","%"+empName+"%");
//        }
//        if(departmentId!=null){
//            criteria.andEqualTo("deptid",departmentId);
//        }
//        if(!StringUtils.isEmpty(moblie)){
//            criteria.andEqualTo("moblie",moblie);
//        }
//        if(!StringUtils.isEmpty(sex)){
//            criteria.andEqualTo("sex",sex);
//        }
//        if(empstatus!=null){
//            criteria.andEqualTo("empstatus",empstatus);
//        }
       // List<Emp> empList= empMapper.selectByExample(o);
        Emp emp = new Emp();
        System.out.println("******22222222222222222222222=====================emp:"+emp);
        BeanUtils.copyProperties(empVO,emp);
        System.out.println("******33333333333333333333333=====================emp:"+emp);
        List<Emp> empList = empMapper.selectEmpList(emp);

        PageInfo<Emp> info=new PageInfo<>(empList);
        return new PageVO<>(info.getTotal(),empList);
    }

    /**
     * 添加员工
     *
     * @param empVO
     */
    @Override
    public void add(EmpVO empVO) {
        Emp emp = new Emp();
        BeanUtils.copyProperties(empVO,emp);
        empMapper.insert(emp);
    }

    /**
     * 编辑员工
     *
     * @param id
     * @return
     */
    @Override
    public EmpVO edit(Integer id) throws SystemException {
        Emp emp = empMapper.selectByPrimaryKey(id);
        if(emp==null){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"编辑的员工不存在");
        }
        //EmpVO empVO = E.converterToGradeVO(emp);
        EmpVO empVO = EmpConverter.converterToEmpVO(emp);
        return empVO;
    }

    /**
     * 更新员工
     *
     * @param id
     * @param empVO
     */
    @Override
    public void update(Integer id, EmpVO empVO) throws SystemException {
        Emp dbEmp = empMapper.selectByPrimaryKey(id);
        if(dbEmp==null){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"编辑的员工信息不存在");
        }
        Emp emp = new Emp();
        BeanUtils.copyProperties(empVO,emp);
        emp.setEmpid(id);
        empMapper.updateByPrimaryKeySelective(emp);
    }

    /**
     * 删除员工
     *
     * @param id
     */
    @Override
    public void delete(Integer id) throws SystemException {
        Emp emp = empMapper.selectByPrimaryKey(id);
        if(emp==null){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"要删除的员工信息不存在");
        }
        empMapper.deleteByPrimaryKey(id);
    }

    /**
     * 所有员工信息
     *
     * @return
     */
    @Override
    public List<EmpVO> findAllVO() {
        List<Emp> emps = empMapper.selectAll();
        //转vo
        List<EmpVO> empVOS = new ArrayList<>();
        if (!CollectionUtils.isEmpty(emps)) {
            for (Emp emp : emps) {
                EmpVO e = new EmpVO();
                BeanUtils.copyProperties(emp, e);
                Example o = new Example(Emp.class);
                o.createCriteria().andEqualTo("empid",emp.getEmpid());
                empVOS.add(e);
            }
        }
        return empVOS;
    }

    /**
     * 全部员工信息
     *
     * @return
     */
    @Override
    public List<Emp> findAll() {
        return empMapper.selectAll();
    }
}
